function drawPPgrid(d2,d3,ppx,ppy,ppz)
clf ;
hold on ;
axis off ;
axis equal ;
n=4;
d=1/n;

%d2=0;
%d3=1;
%ppx=1;
%ppy=1;
%ppz=1;

edgecolor='b';
linewidth=4;
markersize2d=200;
markeredgecolor2d='k';
markerfacecolor2d='r';
%markeredgecolor3d='none';
%markerfacecolor3d='r';
%markercontourcolor3d='k';


mec='none';
mfc='r';
markersize3d=1/(2*n);
edgesize3d=1/(6*n);
phigran=24;
markerthetagran=24;
edgethetagran=24;
efc='b';
eec='none';

eas=0.9;
eds=0.9;
mas=0.5;
mds=0.5;

if d2
    set(gcf,'renderer','painters');
    if ppx
        for x=0:d:1
            for z = -x:2*x:x
                plot(x*[-1,1],z*[1,1],edgecolor,'LineWidth',linewidth);
            end
            plot(2*(x-0.5)*[-1,1],[1,-1],edgecolor,'LineWidth',linewidth);
        end
        for x=d:d:1
            X=-x:2*x*d:x;
            Y=-x:2*x:x;
            [xx,yy]=ndgrid(X,Y);
            scatter(xx(:),yy(:),markersize2d,'MarkerEdgeColor',markeredgecolor2d,'MarkerFaceColor',markerfacecolor2d);
        end
    end
    
    if ppy
        for x=0:d:1
            if x > 0
                for z = -x:2*x:x
                    plot(x*[1,1],z*[-1,1],edgecolor,'LineWidth',linewidth);
                    plot(-x*[1,1],z*[-1,1],edgecolor,'LineWidth',linewidth);
                end
            end
            plot([1,-1],2*(x-0.5)*[-1,1],edgecolor,'LineWidth',linewidth);
        end
        for x=d:d:1
            X=-x:2*x*d:x;
            Y=-x:2*x:x;
            [yy,xx]=ndgrid(Y,X);
            scatter(yy(:),xx(:),markersize2d,'MarkerEdgeColor',markeredgecolor2d,'MarkerFaceColor',markerfacecolor2d);
        end
    end
    
    scatter(0,0,markersize2d,'MarkerEdgeColor',markeredgecolor2d,'MarkerFaceColor',markerfacecolor2d);
end

if d3

    r=edgesize3d;
    theta=0:pi/edgethetagran:2*pi;
    Xc=r*cos(theta);
    Yc=r*sin(theta);
    
    set(gcf,'renderer','OpenGL');

    if ppx
        for x=n*d:n*d:3*n/2
            for y = -1/(2*d):1/(2*d)
                for z = -x:2*x:x
                    surf(x*[-ones(size(Xc)) ; ones(size(Xc))],[Yc+y*d*x*2;Yc+y*d*x*2],[Xc+z;Xc+z],...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',eas,'DiffuseStrength',eds,...
                        'FaceColor',efc,'EdgeColor',eec);
                    surf([Yc+y*d*x*2;Yc+y*d*x*2],x*[-ones(size(Xc)) ; ones(size(Xc))],[Xc+z;Xc+z],...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',eas,'DiffuseStrength',eds,...
                        'FaceColor',efc,'EdgeColor',eec);
                end
            end
        end
    end
    
    if ppy
        for x=n*d:n*d:3*n/2
            for y = -1/(2*d):1/(2*d)
                for z = -x:2*x:x
                    surf(x*[-ones(size(Xc)) ; ones(size(Xc))],[Xc+z;Xc+z],[Yc+y*d*x*2;Yc+y*d*x*2],...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',eas,'DiffuseStrength',eds,...
                        'FaceColor',efc,'EdgeColor',eec);
                    surf([Yc+y*d*x*2;Yc+y*d*x*2],[Xc+z;Xc+z],x*[-ones(size(Xc)) ; ones(size(Xc))],...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',eas,'DiffuseStrength',eds,...
                        'FaceColor',efc,'EdgeColor',eec);
                end
            end
        end
    end

    if ppz
        for x=n*d:n*d:3*n/2
            for y = -1/(2*d):1/(2*d)
                for z = -x:2*x:x
                    surf([Xc+z;Xc+z],x*[-ones(size(Xc)) ; ones(size(Xc))],[Yc+y*d*x*2;Yc+y*d*x*2],...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',eas,'DiffuseStrength',eds,...
                        'FaceColor',efc,'EdgeColor',eec);
                    surf([Xc+z;Xc+z],[Yc+y*d*x*2;Yc+y*d*x*2],x*[-ones(size(Xc)) ; ones(size(Xc))],...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',eas,'DiffuseStrength',eds,...
                        'FaceColor',efc,'EdgeColor',eec);
                end
            end
        end
    end

    r=markersize3d;
    theta=0:pi/markerthetagran:2*pi;
    phi=0:pi/phigran:pi;
    Xs=r*sin(phi')*cos(theta);
    Ys=r*sin(phi')*sin(theta);
    Zs=r*cos(phi');
    Zs=repmat(Zs,1,size(theta,2));
    %XX=-x:2*x*d:x;
    %YY=-x:2*x*d:x;
    %ZZ=-x:2*x:x;

    if ppx
        for x=n*d:n*d:3*n/2
            for xx = -x:2*x*d:x
                for yy = -x:2*x*d:x
                    for zz = -x:2*x:x
                        surf(Xs+xx,Ys+yy,Zs+zz,...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',mas,'DiffuseStrength',mds,...
                        'EdgeColor',mec,'FaceColor',mfc);
                        if x == 3*n/2
                            surf([Xc;Xc+xx],[Yc;Yc+yy],zz*[zeros(size(Xc)) ; ones(size(Xc))],...
                            'FaceLighting','phong','EdgeLighting','phong',...
                            'AmbientStrength',eas,'DiffuseStrength',eds,...
                            'FaceColor',efc,'EdgeColor',eec);
                        end
                    end
                end
            end
        end
    end

    if ppy
        for x=n*d:n*d:3*n/2
            for xx = -x:2*x*d:x
                for yy = -x:2*x*d:x
                    for zz = -x:2*x:x
                        surf(Xs+xx,Zs+zz,Ys+yy,...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',mas,'DiffuseStrength',mds,...
                        'EdgeColor',mec,'FaceColor',mfc);
                        if x == 3*n/2
                            surf([Xc;Xc+xx],zz*[zeros(size(Xc)) ; ones(size(Xc))],[Yc;Yc+yy],...
                            'FaceLighting','phong','EdgeLighting','phong',...
                            'AmbientStrength',eas,'DiffuseStrength',eds,...
                            'FaceColor',efc,'EdgeColor',eec);
                        end
                    end
                end
            end
        end
    end

    if ppz
        for x=n*d:n*d:3*n/2
            for xx = -x:2*x*d:x
                for yy = -x:2*x*d:x
                    for zz = -x:2*x:x
                        surf(Zs+zz,Xs+xx,Ys+yy,...
                        'FaceLighting','phong','EdgeLighting','phong',...
                        'AmbientStrength',mas,'DiffuseStrength',mds,...
                        'EdgeColor',mec,'FaceColor',mfc);
                        if x == 3*n/2
                            surf(zz*[zeros(size(Xc)) ; ones(size(Xc))],[Xc;Xc+xx],[Yc;Yc+yy],...
                            'FaceLighting','phong','EdgeLighting','phong',...
                            'AmbientStrength',eas,'DiffuseStrength',eds,...
                            'FaceColor',efc,'EdgeColor',eec);
                        end
                    end
                end
            end
        end
    end
    
    surf(Zs,Xs,Ys,...
        'FaceLighting','phong','EdgeLighting','phong',...
        'AmbientStrength',mas,'DiffuseStrength',mds,...
        'EdgeColor',mec,'FaceColor',mfc);

    view(-10,8);
    camlight('headlight');
end